package com.csw.shuanfa.other.socket;

import lombok.extern.slf4j.Slf4j;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * localhost 10001
 *
 * @author 1
 */
@Slf4j
public class GreetingClient {

    public static void main(String[] args) {
        String serverName = args[0];
        int port = Integer.parseInt(args[1]);
        try {
            log.info("" + "连接到主机：" + serverName + " ，端口号：" + port);
            try (Socket client = new Socket(serverName, port)) {
                log.info("" + "远程主机地址：" + client.getRemoteSocketAddress());
                OutputStream outToServer = client.getOutputStream();
                DataOutputStream out = new DataOutputStream(outToServer);

                out.writeUTF("Hello from " + client.getLocalSocketAddress());
                InputStream inFromServer = client.getInputStream();
                DataInputStream in = new DataInputStream(inFromServer);
                log.info("" + "服务器响应： " + in.readUTF());
                client.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
